// 武器系统
class Weapon {
    constructor(type, currentAmmo, totalAmmo, fireRate, damage) {
        this.type = type;
        this.currentAmmo = currentAmmo;
        this.totalAmmo = totalAmmo;
        this.fireRate = fireRate; // 毫秒
        this.damage = damage;
        this.lastShotTime = 0;
        this.reloadTime = 2000; // 重新装弹时间
        this.isReloading = false;
        this.reloadStartTime = 0;
        
        // 武器属性
        this.weaponStats = this.getWeaponStats(type);
        
        // 掉落物品属性
        this.x = 0;
        this.y = 0;
        this.isDropped = false;
    }
    
    getWeaponStats(type) {
        const stats = {
            'pistol': {
                name: '手枪',
                damage: 25,
                fireRate: 300,
                reloadTime: 1500,
                clipSize: 12,
                maxAmmo: 120,
                bulletSpeed: 800,
                icon: '🔫'
            },
            'rifle': {
                name: '步枪',
                damage: 35,
                fireRate: 150,
                reloadTime: 2500,
                clipSize: 30,
                maxAmmo: 180,
                bulletSpeed: 1000,
                icon: '🎯'
            },
            'shotgun': {
                name: '霰弹枪',
                damage: 60,
                fireRate: 800,
                reloadTime: 3000,
                clipSize: 6,
                maxAmmo: 48,
                bulletSpeed: 600,
                icon: '💥'
            },
            'smg': {
                name: '冲锋枪',
                damage: 20,
                fireRate: 100,
                reloadTime: 2000,
                clipSize: 25,
                maxAmmo: 200,
                bulletSpeed: 700,
                icon: '🔥'
            },
            'sniper': {
                name: '狙击枪',
                damage: 100,
                fireRate: 1500,
                reloadTime: 3500,
                clipSize: 5,
                maxAmmo: 30,
                bulletSpeed: 1200,
                icon: '🎯'
            },
            'minigun': {
                name: '机关枪',
                damage: 15,
                fireRate: 80,
                reloadTime: 4000,
                clipSize: 100,
                maxAmmo: 400,
                bulletSpeed: 900,
                icon: '🔫'
            },
            'laser': {
                name: '激光枪',
                damage: 45,
                fireRate: 200,
                reloadTime: 1000,
                clipSize: 20,
                maxAmmo: 100,
                bulletSpeed: 1500,
                icon: '⚡'
            },
            'plasma': {
                name: '等离子枪',
                damage: 70,
                fireRate: 400,
                reloadTime: 2500,
                clipSize: 8,
                maxAmmo: 48,
                bulletSpeed: 800,
                icon: '💥'
            }
        };
        
        return stats[type] || stats['pistol'];
    }
    
    update(deltaTime) {
        if (this.isReloading) {
            if (Date.now() - this.reloadStartTime >= this.reloadTime) {
                this.finishReload();
            }
        }
    }
    
    canShoot() {
        return !this.isReloading && 
               this.currentAmmo > 0 && 
               (Date.now() - this.lastShotTime >= this.fireRate);
    }
    
    shoot(x, y, targetX, targetY) {
        if (!this.canShoot()) {
            return null;
        }
        
        // 计算射击方向
        const dx = targetX - x;
        const dy = targetY - y;
        const distance = Math.sqrt(dx * dx + dy * dy);
        
        if (distance === 0) return null;
        
        const dirX = dx / distance;
        const dirY = dy / distance;
        
        this.currentAmmo--;
        this.lastShotTime = Date.now();
        
        // 记录射击统计
        if (window.storageManager) {
            window.storageManager.updateStatistics('bulletFired');
            window.storageManager.updateStatistics('weaponUsed', 1, this.type);
        }
        
        // 根据武器类型创建子弹
        if (this.type === 'shotgun') {
            return this.createShotgunBullets(x, y, dirX, dirY);
        } else if (this.type === 'laser') {
            return this.createLaserBeam(x, y, dirX, dirY);
        } else if (this.type === 'plasma') {
            return this.createPlasmaBolt(x, y, dirX, dirY);
        } else {
            return new Bullet(
                x, y, 
                dirX * this.weaponStats.bulletSpeed, 
                dirY * this.weaponStats.bulletSpeed,
                this.weaponStats.damage
            );
        }
    }
    
    createShotgunBullets(x, y, dirX, dirY) {
        const bullets = [];
        const spreadAngle = Math.PI / 6; // 30度扩散
        const pelletsCount = 5;
        
        for (let i = 0; i < pelletsCount; i++) {
            const angle = Math.atan2(dirY, dirX) + 
                         (Math.random() - 0.5) * spreadAngle;
            const bulletDirX = Math.cos(angle);
            const bulletDirY = Math.sin(angle);
            
            bullets.push(new Bullet(
                x, y,
                bulletDirX * this.weaponStats.bulletSpeed,
                bulletDirY * this.weaponStats.bulletSpeed,
                this.weaponStats.damage / pelletsCount
            ));
        }
        
        return bullets;
    }
    
    createLaserBeam(x, y, dirX, dirY) {
        return new LaserBeam(x, y, dirX, dirY, this.weaponStats.damage);
    }
    
    createPlasmaBolt(x, y, dirX, dirY) {
        return new PlasmaBolt(
            x, y, 
            dirX * this.weaponStats.bulletSpeed, 
            dirY * this.weaponStats.bulletSpeed,
            this.weaponStats.damage
        );
    }
    
    reload() {
        if (this.isReloading || this.currentAmmo === this.weaponStats.clipSize) {
            return;
        }
        
        if (this.totalAmmo === 0) {
            return; // 没有弹药
        }
        
        this.isReloading = true;
        this.reloadStartTime = Date.now();
        this.reloadTime = this.weaponStats.reloadTime;
    }
    
    finishReload() {
        const ammoNeeded = this.weaponStats.clipSize - this.currentAmmo;
        const ammoToReload = Math.min(ammoNeeded, this.totalAmmo);
        
        this.currentAmmo += ammoToReload;
        this.totalAmmo -= ammoToReload;
        this.isReloading = false;
    }
    
    // 快速换弹系统 - 1秒完成，只要有子弹就能换弹
    startQuickReload() {
        if (this.isReloading || this.totalAmmo <= 0) {
            return;
        }
        
        this.isReloading = true;
        this.reloadStartTime = Date.now();
        this.reloadTime = 1000; // 1秒快速换弹
        
    }
    
    // 作为掉落物品
    drop(x, y) {
        this.x = x;
        this.y = y;
        this.isDropped = true;
    }
    
    render(ctx) {
        if (!this.isDropped) return;
        
        ctx.save();
        ctx.translate(this.x, this.y);
        
        // 检查是否为增强版武器
        const baseStats = this.getWeaponStats(this.type);
        const isEnhanced = this.weaponStats.damage > baseStats.damage;
        
        if (isEnhanced) {
            // 增强版武器的特殊发光效果
            ctx.shadowColor = '#00FFFF';
            ctx.shadowBlur = 15 + Math.sin(Date.now() * 0.01) * 5;
            
            // 环绕光环效果
            const glowRadius = 25 + Math.sin(Date.now() * 0.005) * 3;
            const gradient = ctx.createRadialGradient(0, 0, 0, 0, 0, glowRadius);
            gradient.addColorStop(0, 'rgba(0, 255, 255, 0.3)');
            gradient.addColorStop(0.7, 'rgba(0, 255, 255, 0.1)');
            gradient.addColorStop(1, 'rgba(0, 255, 255, 0)');
            
            ctx.fillStyle = gradient;
            ctx.fillRect(-glowRadius, -glowRadius, glowRadius * 2, glowRadius * 2);
        }
        
        // 绘制武器掉落物品
        this.drawDroppedWeapon(ctx);
        
        // 绘制拾取提示
        this.drawPickupHint(ctx, isEnhanced);
        
        ctx.restore();
    }
    
    drawDroppedWeapon(ctx) {
        // 武器像素艺术
        ctx.fillStyle = '#666666';
        ctx.fillRect(-12, -3, 24, 6);
        
        // 武器细节
        ctx.fillStyle = '#444444';
        ctx.fillRect(-10, -2, 20, 4);
        
        // 枪口
        ctx.fillStyle = '#000000';
        ctx.fillRect(10, -1, 4, 2);
        
        // 武器类型标识
        ctx.fillStyle = '#FFFFFF';
        ctx.font = '12px monospace';
        ctx.textAlign = 'center';
        ctx.fillText(this.weaponStats.icon, 0, -15);
    }
    
    drawPickupHint(ctx, isEnhanced = false) {
        // 绘制拾取提示
        ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
        ctx.font = '10px monospace';
        ctx.textAlign = 'center';
        ctx.fillText('按 E 拾取', 0, 25);
        
        if (isEnhanced) {
            ctx.fillStyle = '#00FFFF';
            ctx.font = 'bold 12px monospace';
            ctx.fillText(`★ ${this.weaponStats.name} ★`, 0, 38);
            
            // 增强版标记
            ctx.fillStyle = '#00FF00';
            ctx.font = '9px monospace';
            ctx.fillText('【强化版】', 0, 50);
            
            // 显示增强属性
            ctx.fillStyle = '#90EE90';
            ctx.font = '8px monospace';
            ctx.fillText(`伤害: ${this.weaponStats.damage}`, 0, 62);
            ctx.fillText(`射速: +25%`, 0, 72);
        } else {
            ctx.fillText(this.weaponStats.name, 0, 35);
        }
    }
}

// 子弹类
class Bullet {
    constructor(x, y, vx, vy, damage) {
        this.x = x;
        this.y = y;
        this.vx = vx;
        this.vy = vy;
        this.damage = damage;
        this.radius = 3;
        this.color = '#FFD700';
        this.trail = [];
        this.maxTrailLength = 5;
    }
    
    update(deltaTime) {
        // 添加拖尾效果
        this.trail.push({ x: this.x, y: this.y });
        if (this.trail.length > this.maxTrailLength) {
            this.trail.shift();
        }
        
        // 更新位置
        this.x += this.vx * deltaTime / 1000;
        this.y += this.vy * deltaTime / 1000;
    }
    
    render(ctx) {
        ctx.save();
        
        // 绘制拖尾
        this.trail.forEach((point, index) => {
            const alpha = (index + 1) / this.trail.length * 0.5;
            ctx.globalAlpha = alpha;
            ctx.fillStyle = this.color;
            ctx.fillRect(point.x - 1, point.y - 1, 2, 2);
        });
        
        ctx.globalAlpha = 1;
        
        // 绘制子弹
        ctx.fillStyle = this.color;
        ctx.fillRect(this.x - this.radius, this.y - this.radius, 
                    this.radius * 2, this.radius * 2);
        
        // 子弹发光效果
        ctx.shadowColor = this.color;
        ctx.shadowBlur = 5;
        ctx.fillRect(this.x - 1, this.y - 1, 2, 2);
        
        ctx.restore();
    }
}

// 激光束类
class LaserBeam {
    constructor(x, y, dirX, dirY, damage) {
        this.startX = x;
        this.startY = y;
        this.dirX = dirX;
        this.dirY = dirY;
        this.damage = damage;
        this.range = 800; // 激光射程
        this.width = 4; // 激光宽度
        this.life = 150; // 激光持续时间（毫秒）
        this.maxLife = this.life;
        this.color = '#00FFFF'; // 青色激光
        this.glowColor = '#FFFFFF';
        
        // 计算激光终点
        this.endX = x + dirX * this.range;
        this.endY = y + dirY * this.range;
    }
    
    // 碰撞检测现在由gameEngine.js中的handleLaserCollisions处理
    
    update(deltaTime) {
        this.life -= deltaTime;
        return this.life > 0;
    }
    
    render(ctx) {
        if (this.life <= 0) return;
        
        ctx.save();
        
        // 计算透明度
        const alpha = this.life / this.maxLife;
        
        // 绘制激光外发光
        ctx.globalAlpha = alpha * 0.3;
        ctx.strokeStyle = this.glowColor;
        ctx.lineWidth = this.width + 4;
        ctx.beginPath();
        ctx.moveTo(this.startX, this.startY);
        ctx.lineTo(this.endX, this.endY);
        ctx.stroke();
        
        // 绘制激光核心
        ctx.globalAlpha = alpha;
        ctx.strokeStyle = this.color;
        ctx.lineWidth = this.width;
        ctx.beginPath();
        ctx.moveTo(this.startX, this.startY);
        ctx.lineTo(this.endX, this.endY);
        ctx.stroke();
        
        // 绘制激光内核
        ctx.globalAlpha = alpha;
        ctx.strokeStyle = '#FFFFFF';
        ctx.lineWidth = this.width / 2;
        ctx.beginPath();
        ctx.moveTo(this.startX, this.startY);
        ctx.lineTo(this.endX, this.endY);
        ctx.stroke();
        
        ctx.restore();
    }
}

// 等离子球类
class PlasmaBolt {
    constructor(x, y, vx, vy, damage) {
        this.x = x;
        this.y = y;
        this.vx = vx;
        this.vy = vy;
        this.damage = damage;
        this.radius = 8;
        this.color = '#FF6B35';
        this.glowColor = '#FFFF00';
        this.trail = [];
        this.maxTrailLength = 8;
        this.energy = 1.0;
        this.pulseFactor = 0;
    }
    
    update(deltaTime) {
        // 添加拖尾效果
        this.trail.push({ x: this.x, y: this.y, energy: this.energy });
        if (this.trail.length > this.maxTrailLength) {
            this.trail.shift();
        }
        
        // 更新位置
        this.x += this.vx * deltaTime / 1000;
        this.y += this.vy * deltaTime / 1000;
        
        // 更新脉冲效果
        this.pulseFactor += deltaTime * 0.01;
        this.energy = 0.8 + Math.sin(this.pulseFactor) * 0.2;
    }
    
    render(ctx) {
        ctx.save();
        
        // 绘制拖尾
        this.trail.forEach((point, index) => {
            const alpha = (index + 1) / this.trail.length * 0.6;
            const size = (index + 1) / this.trail.length * this.radius;
            
            ctx.globalAlpha = alpha;
            ctx.fillStyle = this.color;
            ctx.beginPath();
            ctx.arc(point.x, point.y, size, 0, Math.PI * 2);
            ctx.fill();
        });
        
        // 绘制等离子球外发光
        ctx.globalAlpha = 0.5;
        ctx.fillStyle = this.glowColor;
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.radius * 1.5 * this.energy, 0, Math.PI * 2);
        ctx.fill();
        
        // 绘制等离子球核心
        ctx.globalAlpha = 1;
        ctx.fillStyle = this.color;
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.radius * this.energy, 0, Math.PI * 2);
        ctx.fill();
        
        // 绘制等离子球内核
        ctx.fillStyle = '#FFFFFF';
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.radius * 0.5 * this.energy, 0, Math.PI * 2);
        ctx.fill();
        
        ctx.restore();
    }
}

// 近战武器类
class MeleeWeapon {
    constructor(type) {
        this.type = type;
        this.stats = this.getMeleeStats(type);
        this.attackSpeed = this.stats.attackSpeed;
        this.damage = this.stats.damage;
        this.range = this.stats.range;
        this.comboMultiplier = this.stats.comboMultiplier;
        this.lastAttack = 0;
        
        // 掉落物品属性
        this.x = 0;
        this.y = 0;
        this.isDropped = false;
        this.bobOffset = Math.random() * Math.PI * 2; // 浮动动画的随机偏移
    }
    
    getMeleeStats(type) {
        const stats = {
            'knife': {
                name: '战术刀',
                damage: 40,
                attackSpeed: 400, // 毫秒
                range: 50, // 最短攻击范围
                comboMultiplier: 1.2,
                icon: '🗡️',
                description: '快速轻型近战武器'
            },
            'bat': {
                name: '钢棒',
                damage: 50,
                attackSpeed: 500,
                range: 65, // 中等攻击范围
                comboMultiplier: 1.1,
                icon: '🏏',
                description: '简单实用的近战武器'
            },
            'sword': {
                name: '长剑',
                damage: 70,
                attackSpeed: 600,
                range: 85, // 较长攻击范围
                comboMultiplier: 1.3,
                icon: '⚔️',
                description: '平衡的近战武器'
            },
            'axe': {
                name: '消防斧',
                damage: 90,
                attackSpeed: 800,
                range: 95, // 最长攻击范围
                comboMultiplier: 1.5,
                icon: '🪓',
                description: '重型近战武器，伤害高'
            }
        };
        
        return stats[type] || stats['knife'];
    }
    
    update(deltaTime) {
        // 近战武器更新逻辑
    }
    
    canAttack() {
        return Date.now() - this.lastAttack >= this.attackSpeed;
    }
    
    attack(x, y, targetX, targetY, combo = 1) {
        if (!this.canAttack()) {
            return null;
        }
        
        this.lastAttack = Date.now();
        
        // 计算攻击方向
        const dx = targetX - x;
        const dy = targetY - y;
        const angle = Math.atan2(dy, dx);
        
        // 计算连击伤害
        const comboDamage = this.damage * Math.pow(this.comboMultiplier, combo - 1);
        
        return {
            range: this.range,
            damage: Math.floor(comboDamage),
            angle: angle,
            type: this.type,
            combo: combo
        };
    }
    
    render(ctx, x, y, facing) {
        ctx.save();
        ctx.translate(x, y);
        ctx.rotate(facing);
        
        // 根据武器类型绘制
        switch (this.type) {
            case 'knife':
                this.drawKnife(ctx);
                break;
            case 'sword':
                this.drawSword(ctx);
                break;
            case 'axe':
                this.drawAxe(ctx);
                break;
            case 'bat':
                this.drawBat(ctx);
                break;
        }
        
        ctx.restore();
    }
    
    drawKnife(ctx) {
        // 刀柄
        ctx.fillStyle = '#8B4513';
        ctx.fillRect(10, -1, 6, 3);
        
        // 刀身
        ctx.fillStyle = '#C0C0C0';
        ctx.fillRect(16, -1, 12, 2);
        
        // 刀尖
        ctx.beginPath();
        ctx.moveTo(28, 0);
        ctx.lineTo(32, 0);
        ctx.lineTo(28, -1);
        ctx.lineTo(28, 1);
        ctx.fill();
    }
    
    drawSword(ctx) {
        // 剑柄
        ctx.fillStyle = '#8B4513';
        ctx.fillRect(8, -2, 8, 4);
        
        // 护手
        ctx.fillStyle = '#2F2F2F';
        ctx.fillRect(15, -3, 2, 6);
        
        // 剑身
        ctx.fillStyle = '#C0C0C0';
        ctx.fillRect(17, -1, 20, 2);
        
        // 剑尖
        ctx.beginPath();
        ctx.moveTo(37, 0);
        ctx.lineTo(42, 0);
        ctx.lineTo(37, -1);
        ctx.lineTo(37, 1);
        ctx.fill();
    }
    
    drawAxe(ctx) {
        // 斧柄
        ctx.fillStyle = '#8B4513';
        ctx.fillRect(5, -1, 20, 2);
        
        // 斧头
        ctx.fillStyle = '#2F2F2F';
        ctx.fillRect(20, -4, 8, 8);
        
        // 斧刃
        ctx.fillStyle = '#C0C0C0';
        ctx.fillRect(24, -4, 6, 8);
    }
    
    drawBat(ctx) {
        // 球棒握柄
        ctx.fillStyle = '#8B4513';
        ctx.fillRect(8, -1, 6, 2);
        
        // 球棒身
        ctx.fillStyle = '#654321';
        ctx.fillRect(14, -2, 16, 4);
        
        // 球棒头
        ctx.fillStyle = '#2F2F2F';
        ctx.fillRect(28, -2, 4, 4);
    }
    
    // 作为掉落物品
    drop(x, y) {
        this.x = x;
        this.y = y;
        this.isDropped = true;
    }
    
    // 渲染掉落的近战武器
    renderDropped(ctx, time = 0) {
        if (!this.isDropped) return;
        
        ctx.save();
        
        // 浮动效果
        const bobHeight = Math.sin(time * 0.003 + this.bobOffset) * 3;
        ctx.translate(this.x, this.y + bobHeight);
        
        // 检查是否为增强版武器（第10关buff武器）
        const isEnhanced = this.stats.damage > this.getMeleeStats(this.type).damage;
        
        if (isEnhanced) {
            // 增强版武器的特殊发光效果
            ctx.shadowColor = '#FFD700';
            ctx.shadowBlur = 15 + Math.sin(time * 0.01) * 5;
            
            // 环绕光环效果
            const glowRadius = 30 + Math.sin(time * 0.005) * 5;
            const gradient = ctx.createRadialGradient(0, 0, 0, 0, 0, glowRadius);
            gradient.addColorStop(0, 'rgba(255, 215, 0, 0.3)');
            gradient.addColorStop(0.7, 'rgba(255, 215, 0, 0.1)');
            gradient.addColorStop(1, 'rgba(255, 215, 0, 0)');
            
            ctx.fillStyle = gradient;
            ctx.fillRect(-glowRadius, -glowRadius, glowRadius * 2, glowRadius * 2);
        }
        
        // 绘制武器掉落物品
        this.drawDroppedMelee(ctx);
        
        // 绘制拾取提示
        this.drawMeleePickupHint(ctx, isEnhanced);
        
        ctx.restore();
    }
    
    drawDroppedMelee(ctx) {
        // 根据武器类型绘制掉落物品
        switch (this.type) {
            case 'knife':
                // 刀身
                ctx.fillStyle = '#C0C0C0';
                ctx.fillRect(-10, -1, 20, 2);
                // 刀柄
                ctx.fillStyle = '#8B4513';
                ctx.fillRect(-15, -2, 8, 4);
                break;
                
            case 'sword':
                // 剑身
                ctx.fillStyle = '#C0C0C0';
                ctx.fillRect(-12, -1, 24, 2);
                // 剑柄
                ctx.fillStyle = '#8B4513';
                ctx.fillRect(-18, -2, 8, 4);
                // 护手
                ctx.fillStyle = '#2F2F2F';
                ctx.fillRect(-12, -3, 2, 6);
                break;
                
            case 'axe':
                // 斧柄
                ctx.fillStyle = '#8B4513';
                ctx.fillRect(-15, -1, 20, 2);
                // 斧头
                ctx.fillStyle = '#2F2F2F';
                ctx.fillRect(3, -4, 8, 8);
                // 斧刃
                ctx.fillStyle = '#C0C0C0';
                ctx.fillRect(7, -4, 6, 8);
                break;
                
            case 'bat':
                // 球棒身
                ctx.fillStyle = '#654321';
                ctx.fillRect(-12, -2, 24, 4);
                // 球棒头
                ctx.fillStyle = '#2F2F2F';
                ctx.fillRect(10, -2, 4, 4);
                break;
        }
        
        // 武器类型图标
        ctx.fillStyle = '#FFFFFF';
        ctx.font = '16px monospace';
        ctx.textAlign = 'center';
        ctx.fillText(this.stats.icon, 0, -20);
        
        // 发光效果
        ctx.shadowColor = '#FFD700';
        ctx.shadowBlur = 8;
        ctx.fillText(this.stats.icon, 0, -20);
        ctx.shadowBlur = 0;
    }
    
    drawMeleePickupHint(ctx, isEnhanced = false) {
        // 绘制拾取提示
        ctx.fillStyle = 'rgba(255, 255, 255, 0.9)';
        ctx.font = '11px monospace';
        ctx.textAlign = 'center';
        ctx.fillText('按 E 拾取', 0, 25);
        
        // 武器名称
        if (isEnhanced) {
            ctx.fillStyle = '#FFD700';
            ctx.font = 'bold 13px monospace';
            ctx.fillText(`★ ${this.stats.name} ★`, 0, 38);
            
            // 增强版标记
            ctx.fillStyle = '#FF6347';
            ctx.font = '10px monospace';
            ctx.fillText('【增强版】', 0, 52);
        } else {
            ctx.fillStyle = '#FFD700';
            ctx.font = '12px monospace';
            ctx.fillText(this.stats.name, 0, 38);
        }
        
        // 武器属性提示
        ctx.fillStyle = isEnhanced ? '#90EE90' : '#CCCCCC';
        ctx.font = '9px monospace';
        const yOffset = isEnhanced ? 14 : 0;
        ctx.fillText(`伤害: ${this.stats.damage}`, 0, 50 + yOffset);
        ctx.fillText(`范围: ${this.stats.range}`, 0, 62 + yOffset);
        
        if (isEnhanced) {
            ctx.fillText(`攻速: ${Math.floor(1000/this.stats.attackSpeed*10)/10}/s`, 0, 74 + yOffset);
        }
    }
}

// 武器生成器
class WeaponSpawner {
    constructor() {
        this.spawnPoints = [];
        this.spawnTimer = 0;
        this.spawnInterval = 15000; // 15秒生成一个武器
        this.meleeSpawnTimer = 0;
        this.meleeSpawnInterval = 12000; // 12秒生成一个近战武器
        this.hasSpawnedThisLevel = false; // 标记本关是否已生成武器
    }
    
    addSpawnPoint(x, y) {
        this.spawnPoints.push({ x, y });
    }
    
    update(deltaTime) {
        // 不持续生成武器，只在关卡开始时生成一次
        // 注释掉持续生成逻辑
        /*
        // 远程武器生成
        this.spawnTimer += deltaTime;
        if (this.spawnTimer >= this.spawnInterval) {
            this.spawnRandomWeapon();
            this.spawnTimer = 0;
        }
        
        // 近战武器生成
        this.meleeSpawnTimer += deltaTime;
        if (this.meleeSpawnTimer >= this.meleeSpawnInterval) {
            this.spawnRandomMeleeWeapon();
            this.meleeSpawnTimer = 0;
        }
        */
    }
    
    // 重置关卡标记，在新关卡开始时调用
    resetForNewLevel() {
        this.hasSpawnedThisLevel = false;
        this.spawnTimer = 0;
        this.meleeSpawnTimer = 0;
    }
    
    spawnRandomWeapon() {
        if (this.spawnPoints.length === 0) return;
        
        // 根据关卡调整武器类型
        const currentLevel = window.gameManager ? window.gameManager.levelManager.currentLevel : 1;
        const weaponTypes = this.getAvailableWeapons(currentLevel);
        
        const randomType = weaponTypes[Math.floor(Math.random() * weaponTypes.length)];
        const randomSpawn = this.spawnPoints[Math.floor(Math.random() * this.spawnPoints.length)];
        
        const weapon = new Weapon(randomType, 
            Math.floor(Math.random() * 20) + 10, // 随机弹药
            Math.floor(Math.random() * 100) + 50, // 随机总弹药
            0, 0
        );
        
        weapon.drop(randomSpawn.x, randomSpawn.y);
        
        if (window.gameEngine) {
            window.gameEngine.weapons.push(weapon);
        }
    }
    
    spawnRandomMeleeWeapon() {
        if (this.spawnPoints.length === 0) return;
        
        // 根据关卡调整近战武器类型
        const currentLevel = window.gameManager ? window.gameManager.levelManager.currentLevel : 1;
        const meleeTypes = this.getAvailableMeleeWeapons(currentLevel);
        
        const randomType = meleeTypes[Math.floor(Math.random() * meleeTypes.length)];
        const randomSpawn = this.spawnPoints[Math.floor(Math.random() * this.spawnPoints.length)];
        
        const meleeWeapon = new MeleeWeapon(randomType);
        meleeWeapon.drop(randomSpawn.x, randomSpawn.y);
        
        if (window.gameEngine) {
            // 添加到近战武器数组（需要创建）
            if (!window.gameEngine.meleeWeapons) {
                window.gameEngine.meleeWeapons = [];
            }
            window.gameEngine.meleeWeapons.push(meleeWeapon);
        }
    }
    
    // 根据关卡获取可用的远程武器
    getAvailableWeapons(level) {
        if (level <= 2) {
            return ['pistol', 'rifle'];
        } else if (level <= 4) {
            return ['pistol', 'rifle', 'shotgun', 'smg'];
        } else if (level <= 6) {
            return ['pistol', 'rifle', 'shotgun', 'smg', 'sniper'];
        } else if (level <= 8) {
            return ['pistol', 'rifle', 'shotgun', 'smg', 'sniper', 'minigun'];
        } else {
            return ['pistol', 'rifle', 'shotgun', 'smg', 'sniper', 'minigun', 'laser', 'plasma'];
        }
    }
    
    // 根据关卡获取可用的近战武器
    getAvailableMeleeWeapons(level) {
        if (level <= 2) {
            return ['knife'];
        } else if (level <= 4) {
            return ['knife', 'bat'];
        } else if (level <= 7) {
            return ['knife', 'bat', 'sword'];
        } else {
            return ['knife', 'bat', 'sword', 'axe'];
        }
    }
}